/*
 * SocketRemoteServer.java   0.0.1    28/set/2009
 * 
 * Copyright (c) 2009 mentalsmash.org
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * The use of the Apache License does not indicate that this project is
 * affiliated with the Apache Software Foundation.
 */ 
package org.mentalsmash.tazio.net.cs.client;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.concurrent.ExecutorService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Implementation of {@link RemoteServer} using sockets to communicate with
 * the server
 * @author Andrea Reale <andrea.reale@gmail.com
 * @version 0.0.1
 * @see RemoteServer
 * @see SocketConnection
 */
public class SocketRemoteServer implements RemoteServer {

    @SuppressWarnings("unused")
    private final static Logger log = LoggerFactory.getLogger(SocketRemoteServer.class);
    
    private final ExecutorService executor;
    private InetSocketAddress address;
    private Connection currentConnection;
    
    public SocketRemoteServer(ExecutorService executor, InetAddress addr, int port) {
	this.executor = executor;
	this.address = new InetSocketAddress(addr, port);
	this.currentConnection = null;
    }
    
    
    /* (non-Javadoc)
     * @see org.mentalsmash.tazio.net.cs.client.RemoteServer#connect(java.net.InetAddress)
     */
    @Override
    public synchronized Connection connect(InetAddress address, int port)
    throws IOException {
	if(isConnected()) {
	    return this.getConnection();
	}
	
	InetSocketAddress oldAddess = this.address;
	Connection connection = null;
	
	this.address = new InetSocketAddress(address, port);
	connection = connect();
	if ( connection == null ) {
	    this.address = oldAddess;
	}
	    
	return connection;	
    }

    /* (non-Javadoc)
     * @see org.mentalsmash.tazio.net.cs.client.RemoteServer#connect()
     */
    @Override
    public synchronized  Connection connect() throws IOException {
	if(isConnected()) {
	    return this.getConnection();
	}
	this.currentConnection = new SocketConnection(executor, this);
	return this.currentConnection;
    }

    /* (non-Javadoc)
     * @see org.mentalsmash.tazio.net.cs.client.RemoteServer#disconnect()
     */
    @Override
    public synchronized void disconnect() {
	this.currentConnection.close();

    }

    /* (non-Javadoc)
     * @see org.mentalsmash.tazio.net.cs.client.RemoteServer#getAddress()
     */
    @Override
    public synchronized InetAddress getAddress() {
	return this.address.getAddress();
    }
    
    public synchronized InetSocketAddress getSocketAddress() {
	return this.address;
    }

    /* (non-Javadoc)
     * @see org.mentalsmash.tazio.net.cs.client.RemoteServer#getConnection()
     */
    @Override
    public synchronized Connection getConnection() {
	return this.currentConnection;
    }
    
    /**
     * Sets the current connection of this RemoteServer.
     * <p>
     * Since this method does not make any check on the validity of the 
     * connection set, this method should be called only by the instance of
     * connection associated to this server
     * </p>
     * @param connection The connection to set
     */
    protected synchronized void setConnection(SocketConnection connection) {
	this.currentConnection = connection;
    }

    /* (non-Javadoc)
     * @see org.mentalsmash.tazio.net.cs.client.RemoteServer#setAddress(java.net.InetAddress)
     */
    @Override
    public synchronized void setAddress(InetAddress address) throws ConnectionStateExeption {
	int oldPort;
	
	if(this.isConnected()) {
	    throw new ConnectionStateExeption("already connected");
	}
	
	oldPort = this.address.getPort();
	this.address = new InetSocketAddress(address, oldPort);
	

    }


    /* (non-Javadoc)
     * @see org.mentalsmash.tazio.net.cs.client.RemoteServer#isConnected()
     */
    @Override
    public synchronized boolean isConnected() {
	return (this.currentConnection != null) && (this.currentConnection.isValid());
    }

}
